package com.dsk.connector.source.utils;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.dsk.connector.commons.trans.FieldSchema;
import com.dsk.connector.commons.utils.ConfigUtils;
import org.apache.kafka.connect.data.Schema;
import org.apache.kafka.connect.data.SchemaBuilder;

import java.util.List;
import java.util.Map;

public class ChangeEventUtils {
    /**
     * 从schema key中获取表名
     * 一般的名称如下："productToBackupDB.testmaster.test_table.Key"
     * @param keyName
     * @return
     */
    public static String retriveTableNamefromKey(String keyName){
        String prefix=ConfigUtils.getConfigItem("server.name");
        String endfix=".Key";
        if(keyName.startsWith(prefix)){
            //由于名称后面要加点，因此多取一个字符
            keyName=keyName.substring(prefix.length()+1);
        }
        if(keyName.endsWith(endfix)){
            keyName=keyName.substring(0,keyName.length()-endfix.length());
        }

        return keyName;

    }

    /**
     * 将schema转换成需要的对象
     * @param fieldArray
     * @return
     */
    public static List<FieldSchema> convertToFieldSchema(JSONArray fieldArray){
        List<FieldSchema> fieldSchemaList=fieldArray.toList(FieldSchema.class);

        return fieldSchemaList;
    }

    public static Map<String,Object> convertFieldValues(List<FieldSchema> fieldSchemaList, JSONObject fieldValues){
//        for(FieldSchema fieldSchema:fieldSchemaList){
//            Schema.Type fieldType=Schema.Type.valueOf(schema.getType());
//            Schema schema=SchemaBuilder.type(fieldType).build();
//            schema.type().getName();
//        }

        return null;
    }
}
